<!DOCTYPE html>
<html lang="en">
    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <script>
            /* array.reduce(callback[, initialValue])
            callback函数接受4个参数: 
                之前值: 如 previous (上一次计算的结果)
                当前值: 如 current
                索引值: 如 index
                数组本身: 如 array

                initialValue: 参数可选,表示初始值
                如果指定了initialValue,则把他作为最初使用的 previous的值
                如果缺省,则使用数组的一个元素作为previous初始值,同时current往后排一位.相比有initialValue
            */
            var arr = [1,2,3,4,5];
            var sum = arr.reduce(function(previous,current,index,array){
                console.log("previous:"+previous);
                console.log("current:"+current);
                console.log("index:"+index);
                console.log("array:"+array);
                console.log('------------------')
                return previous + current;
            });
            console.log(sum);

            //这里指定了初始值10
            var sum2 = arr.reduce(function(previous,current,index,array){
                console.log("previous:"+previous);
                console.log("current:"+current);
                console.log("index:"+index);
                console.log("array:"+array);
                console.log('------------------')
                return previous + current;
            },10);
             console.log(sum2);
        </script>   
    </body>
</html>